package com.kai.steering.data.service.entity.domainObject;

import com.kai.steering.data.service.entity.dto.*;
import com.kai.steering.data.service.entity.po.Test;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.Arrays;
import java.util.Date;

import lombok.*;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 * 试卷 Do对象
 * </pre>
 *
 * @author kai
 * @date 2022-02-23
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@ToString
@EqualsAndHashCode(callSuper = false)
@ApiModel(value = "TestDO对象", description = "试卷业务实体")
public class TestDO implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "试卷id")
    private String testId;
    @ApiModelProperty(value = "试卷名")
    private String testName;
    @ApiModelProperty(value = "管理员id或者用户id")
    private String founder;
    @ApiModelProperty(value = "对试卷的介绍")
    private String testInformation;
    @ApiModelProperty(value = "创建时间")
    private Date creationDatetime;
    @ApiModelProperty(value = "知识点")
    private List<KnowledgeDO> knowledgeDOs;
    @ApiModelProperty(value = "题目列表")
    private List<QuestionDO> questions;
    @ApiModelProperty(value = "存在0删除1")
    private Integer usable;
    @ApiModelProperty(value = "删除日期")
    private Date deleteDatetime;

    public TestDO(TestQueryDTO testQueryDTO) {
        this.convertTestDO(testQueryDTO);
    }
    public TestDO(TestDTO testDTO) {
        this.convertTestDO(testDTO);
    }
    /**
     * @param test
     */
    public TestDO(Test test) {
        this.convertTestDO(test);
    }

    public TestDO convertTestDO(Test test) {
        if (test==null){
            return null;
        }
        this.testId = test.getTestId();
        this.testName = test.getTestName();
        this.founder = test.getFounder();
        this.testInformation = test.getTestInformation();
        this.creationDatetime = test.getCreationDatetime();
        if (test.getKnowledgeId()!=null){
            this.knowledgeDOs= KnowledgeDO.converKnowledgeDO(test.getKnowledgeId());
        }
        this.usable = test.getUsable();
        this.deleteDatetime = test.getDeleteDatetime();
        return this;
    }

    public TestDO convertTestDO(TestQueryDTO testQueryDTO) {
        this.testId = testQueryDTO.getTestId();
        this.testName = testQueryDTO.getTestName();
        this.founder = null;
        this.testInformation = testQueryDTO.getTestInformation();
        this.creationDatetime = null;
        this.knowledgeDOs = testQueryDTO.getKnowledges().stream().map(knowledgeQueryDTO -> {
            return new KnowledgeDO(knowledgeQueryDTO);
        }).collect(Collectors.toList());
        this.questions = testQueryDTO.getQuestions().stream().map(questionQueryDTO -> {
            return new QuestionDO(questionQueryDTO);
        }).collect(Collectors.toList());
        this.deleteDatetime = getDeleteDatetime();
        return this;
    }

    public TestDO convertTestDO(TestDTO testDTO) {
        this.testId = testDTO.getTestId();
        this.testName = testDTO.getTestName();
        this.founder = testDTO.getFounder();
        this.testInformation = testDTO.getTestInformation();
        this.creationDatetime = null;
        if (testDTO.getKnowledges()!=null) {
            this.knowledgeDOs = Arrays.stream(testDTO.getKnowledges()).map
                    ( knowledgeS-> {
                        return new KnowledgeDO(knowledgeS);
                    }).collect(Collectors.toList());
        }
        this.deleteDatetime = getDeleteDatetime();
        return this;
    }
}